
<?php
include_once ('functions/getTrendingWords.php');
include_once ('functions/search.php');
include_once ('functions/newsAreRelated.php');
include_once('classes/News.php');

function getTrendingNews(){
	$NewsToReturn  = 5;
	$trendingWordsArray = getTrendingWords();
	$trendingNewsArray = array();
	$NonRelatedNewsFound = 0;
	$index = 0;
	$trendingWords = count($trendingWordsArray['words']);
	$metodo = 'peso';
	
	while($NonRelatedNewsFound < $NewsToReturn && $index < $trendingWords){
		$trendingword = $trendingWordsArray['words'][$index];
		$newsArray = search($trendingword,$metodo);
		//I'll just use the one with more 'weight'
		if (count($newsArray) > 0){
			$isValidNews = ($newsArray[0] instanceof News);
			if ($isValidNews){
			
				$news = $newsArray[0];
				
				$similarNewsAlreadyListed = newsIsSimilarToTheOnesInArray($news,$trendingNewsArray);
				
				if (!$similarNewsAlreadyListed){
					$trendingNewsArray[]=$news;
				}
			}
		}
		$NonRelatedNewsFound = count($trendingNewsArray);
		$index++;
	}
	
	return($trendingNewsArray);

}

function newsIsSimilarToTheOnesInArray($newsToCheck,$newsArray){
	$similar = FALSE;
	$j = 0;
	$limit  = count($newsArray);
	while ($j < $limit && !$similar){
		$similar = newsAreRelated($newsToCheck,$newsArray[$j]);
		$j++;
	}
	
	return $similar;
}
?>
